package com.example.makotoshoda.myapplication;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TableRow;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by makoto.shoda on 2014/12/29.
 */
public class MyRow {
    protected final MainActivity mActivity;
    private TableRow mRow;
    int mId;
    int mCount;
    private TextView mCounter;
    private static ArrayList<MyRow> mRows = new ArrayList<MyRow>();
    private View view;
    private int mSort;

    public MyRow(MainActivity activity, int id, String name, int count) {
        this.mActivity = activity;
        mRow = (TableRow) View.inflate(this.mActivity, R.layout.myrow, null);
        mId = id;
        TextView input = (TextView) mRow.findViewById(R.id.lblName);
        input.setText(name);
        mCount = count;
        mCounter = (TextView) mRow.findViewById(R.id.lblCount);
        mCounter.setText(String.valueOf(mCount));
        Button btn = (Button) mRow.findViewById(R.id.btnDown);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCount--;
                MyRow.this.update();
            }
        });
        btn = (Button) mRow.findViewById(R.id.btnUp);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCount++;
                MyRow.this.update();
            }
        });

        btn = (Button) mRow.findViewById(R.id.btnSetting);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyRow.this.mActivity, RowActivity.class);
                intent.putExtra("id", MyRow.this.mId);
                MyRow.this.mActivity.startActivityForResult(intent, 1);
            }
        });

        // 自分を追加する
        mRows.add(this);
    }

    public String getName() {
        return ((TextView)mRow.findViewById(R.id.lblName)).getText().toString();
    }

    public int getCount() {
        return mCount;
    }

    public void update() {
        mCounter.setText(String.valueOf(mCount));
    }

    static void clear() {
        mRows.clear();
    }

    static MyRow getRowByName(String name) {
        for (MyRow row : mRows) {
            if (row.getName().equals(name)) {
                return row;
            }
        }
        return null;
    }

    protected void save(MyDB db) {
        db.saveUser(getName(), getCount(), getSort(), 0);
    }

    static void saveAll(MyDB db) {
        db.deleteUsers();
        for (MyRow row : mRows) {
            row.save(db);
        }
    }

    public View getView() {
        return mRow;
    }

    public int getSort() {
        return mSort;
    }

    public int getId() {
        return mId;
    }
}
